package vis.app.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Observable;
import java.util.Queue;

import vis.app.utils.Utils;

public class CodeController extends Observable {
	public static final String JOBS_ANIMATION = "JOBS_ANIMATION";

	private static CodeController _instance = null;

	public static CodeController getInstance() {
		if (null == _instance) {
			_instance = new CodeController();
		}
		return _instance;
	}

	public ArrayList<String> codeText = new ArrayList<String>();
	public HashMap<String, String> debugVariables = new HashMap<String, String>();
	public Queue<String[]> highlightJobs = new LinkedList<String[]>();

	public void loadCodeText(String codeFileName) {
		codeText = Utils.readCodeFile(codeFileName);
		setDefautHighlight();
	}

	private void setDefautHighlight() {
		String[] defaultJobs = { "0", "1", "2", "3" };
		highlightJobs.add(defaultJobs);
	}

	public boolean hasHighlightJobs() {
		return !highlightJobs.isEmpty();
	}

	public void addHighlightJob(String[] lineNumbers) {
		highlightJobs.add(lineNumbers);
	}

	public String[] getNextHighlightJobs() {
		String[] nextJobs = {};
		if (!highlightJobs.isEmpty()) {
			nextJobs = highlightJobs.poll();
		}
		return nextJobs;
	}

	public void startPushJobs() {
		highlightJobs.clear();
	}

	public void endPushJobs() {
		// dumpHighlightJobs();
	}

	private void dumpHighlightJobs() {
		System.out.println("Current jobs: ");
		for (String[] ss : highlightJobs) {
			String oneJobList = "";
			for (String s : ss) {
				oneJobList += s + ", ";
			}
			System.out.println(oneJobList + "\n");
		}
	}

	public void executeOneHighlightJob() {
		String[] newJobs = getNextHighlightJobs();
		if (0 == newJobs.length) {
			return;
		}

		EventControl evt = new EventControl(JOBS_ANIMATION, "", "");
		evt.setData(newJobs);
		setChanged();
		notifyObservers(evt);
	}

	public void addDebugVariable(String varName, String value) {
		debugVariables.put(varName, value);
	}
}
